import {setStorage, getStorage, removeStorage} from "@/util/auth";
import {getAuthorize,getLocation} from '@/util/util.js'
import Vue from "vue";
const state={
    position: getStorage('position'),
}
const mutations={
	SET_POSITION(state,position){
        if(position===null){
            removeStorage('position');
        }else{
            setStorage('position',position)
        }
        state.position=position;
		uni.$emit('position',position)
    }
}
const actions={
	getPosition({commit}){
		return new Promise(async (resolve, reject) => {
		    await getAuthorize('scope.userLocation','定位')
			const position=await getLocation()
			const latitude=position.latitude;
			const longitude=position.longitude
			Vue.prototype.qqMap.reverseGeocoder({
				location:{
					latitude,longitude
				},
				get_poi:1,
				success(e){
					const data={latitude,longitude,address:e.result.address_reference.landmark_l2.title.replace('酒泉市','')};
					commit('SET_POSITION',data)
					resolve(data)
				},
				fail(e){
					console.log(e)
					uni.showToast({
						title:'位置解析失败',
						icon:'none',
					})
				}
			})
		})
	},
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
